package threadsafe;

import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *Description:
 *Version:1.0.0
 *@author shihb
 *@date 2020/5/23 22:51
 */
public class ListSafe {

  public static void main(String[] args) {
//    java.util.ConcurrentModificationException 线程修改异常
    /**
     *线程不安全解决方法：
     * 1.new Vector<>() synchronized
     * 2.Collections.synchronizedList
     * 3.new CopyOnWriteArrayList<>();
     */

//    ArrayList<String> arr = new ArrayList<>();
//    Vector<String> arr = new Vector<>();
//    List<Object> arr = Collections.synchronizedList(new ArrayList<>());
    CopyOnWriteArrayList<String> arr = new CopyOnWriteArrayList<>();
    for (int i = 0; i < 10; i++) {
      new Thread(()->{
        arr.add(UUID.randomUUID().toString().substring(0,5));
        System.out.println(arr);
      }).start();
    }
  }
}
